package com.hicorp.segment.service.impl;

import com.hicorp.segment.mapper.AnnexMapper;
import com.hicorp.segment.pojo.po.Annex;
import com.hicorp.segment.security.utils.SecurityUtils;
import com.hicorp.segment.service.AnnexService;
import com.hicorp.segment.utils.ResultBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
import java.util.List;

@Service("annexService")
@Slf4j
public class AnnexServiceImpl implements AnnexService {

    @Resource
    private AnnexMapper annexMapper;

    private final SecurityUtils securityUtils;

    public AnnexServiceImpl(SecurityUtils securityUtils) {
        this.securityUtils = securityUtils;
    }

    /**
     * 上传附件
     * @param file      上传的文件
     * @param tableName 关联的表名
     * @return 上传文件的文件名
     */
    @Override
    public ResultBean<Void> uploadAnnex(MultipartFile file, String tableName, Long rowId) {
        if (file.isEmpty()) {
            return new ResultBean<>(400, "上传失败, 请选择文件!");
        }
        String fileName = file.getOriginalFilename();
        File dest = new File("D:\\annex\\" + tableName + "\\" + fileName);
        boolean isMkdirsSuccess = true;
        if (!dest.exists()) {
            isMkdirsSuccess = dest.mkdirs();
        }
        if (!isMkdirsSuccess) {
            return new ResultBean<>(400, "在服务器上创建目录失败, 请联系管理员!");
        }
        try {
            file.transferTo(dest);
            Date date = new Date();
            Annex annex = new Annex(tableName, rowId, fileName, dest.getPath(), securityUtils.getUsername(), date);
            annexMapper.insert(annex);
            log.info("上传成功");
            return new ResultBean<>(200, "上传文件: " + fileName + "成功!");
        } catch (Exception e) {
            log.error(e.getMessage());
            return new ResultBean<>(400, "上传文件失败, 请联系管理员!");
        }
    }

    @Override
    public ResultBean<List<Annex>> selectAll(String tableName, Long rowId) {
        try {
            List<Annex> annexList = annexMapper.selectByTableNameAndRowId(tableName, rowId);
            return new ResultBean<>(annexList);
        } catch (Exception e) {
            log.error(e.getMessage());
            return new ResultBean<>(400, "查询失败!");
        }
    }

    @Override
    public ResultBean<Long> deleteSingleAnnex(String tableName, Long id, Annex annex) {
        String filePath = annex.getPath();
        File file = new File(filePath);
        boolean isDelete;
        if (file.exists()) {
             isDelete = file.delete();
        } else {
            isDelete = true;
        }
        if (isDelete) {
            try {
                annexMapper.deleteAnnex(tableName, id);
                return new ResultBean<>(id);
            } catch (Exception e) {
                log.error(e.getMessage());
                return new ResultBean<>(400, "删除数据库信息失败!");
            }
        } else {
            return new ResultBean<>(400, "文件删除失败!");
        }
    }
}
